package pl.uz.zgora.plan;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BuildingViewActivity extends Activity {
	
	private static String ulica = null;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.building_view);
	    
	    final Bundle intentParams = getIntent().getExtras();
	    final String builidingName = intentParams.getString("budynek");
	    
	    if(builidingName.equals("Uwagi") || builidingName == null) {
	    	AlertDialog dialog = HelperMethodsManager.getErrorDialog(this, "Błąd. Nieprawidłowy budynek", this);
	    	dialog.show();
	    }
	    
	    final PlanUzDataSource planDataSource = new PlanUzDataSource(this);
	    
	    String [] budynkiArr = new String[] { builidingName };
	    
	    ArrayList<Budynek> budynki = planDataSource.readBudynki(budynkiArr);
	    if(!budynki.isEmpty()) { 
	    	Budynek budynek = budynki.get(0);
	    	
	    	TextView tv = (TextView) findViewById(R.id.buildingName);
	    	tv.setText("Budynek: " + budynek.getIdBudynku());
	    	
	    	String buildingName = budynek.getIdBudynku();
	    	buildingName = buildingName.replace("-", "");
	    	buildingName = buildingName.toLowerCase();
	    	showImage(buildingName);
	    	
	    	tv = (TextView) findViewById(R.id.department);
	    	tv.setText(budynek.getWydzial());

	    	ulica = budynek.getUlica();
	    	
	    	tv = (TextView) findViewById(R.id.address);
	    	tv.setText(ulica + ", " + budynek.getKod_pocztowy() + " " + budynek.getMiasto());
	    } else {
	    	AlertDialog dialog = HelperMethodsManager.getErrorDialog(this, "Błąd. Brak budynku w bazie danych.", this);
	    	dialog.show();	    	
	    }
	    
	    final Button navButton = (Button) findViewById(R.id.navButton);
	    navButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(ulica != null) {
					HelperMethodsManager.startGoogleMaps(ulica, BuildingViewActivity.this);
				}
			}
		});
	}
	
	private void showImage(final String filename) {
		final String uri = "drawable/" + filename;

		final ImageView imageView = (ImageView) findViewById(R.id.buildingImage);
		Drawable image = null;

		int imageResource = getResources().getIdentifier(uri, null,
				getPackageName());
		if (imageResource != 0) {
			image = getResources().getDrawable(imageResource);
		} else { image = getResources().getDrawable(R.drawable.blank); }
		imageView.setImageDrawable(image);
	}

}
